package com.hxzy.tms.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.hxzy.tms.dao.BaseDao.Params;
import com.hxzy.tms.dao.ProjectDao;
import com.hxzy.tms.dao.TeamDao;
import com.hxzy.tms.entity.Project;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.service.ProjectService;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.session.SessionHolder;

public class ProjectServiceImpl implements ProjectService {

	private ProjectDao projectDao;
	private TeamDao teamDao;
	
	public ServiceResult<List<Project>> read(Long id) {
		if(SessionHolder.get().getId()==null){
			return new ServiceResult<List<Project>>(-1,null,"未登陸用戶");
		}
		List<Project> projectList=projectDao.read("from Project where id=:id", new Params("id",id));
		return new ServiceResult<List<Project>>(1,projectList);
	}
	public ServiceResult<List<Project>> projectList(Long id){
		if(SessionHolder.get().getId()==null){
			return new ServiceResult<List<Project>>(-1,null,"未登陸用戶");
		}
		List<Project> projectList=projectDao.read("from Project where team.id=:id", new Params("id",id));
		return new ServiceResult<List<Project>>(1,projectList);
	}
	
	
	//创建项目
	public ServiceResult<Project> create(Long teamId, String title, String desc) {
		return saveOrUpdate(null, teamId, title, desc);
	}

	public ServiceResult<Project> update(Long id, Long teamId, String title, String desc) {
		// TODO Auto-generated method stub
		return saveOrUpdate(id, teamId, title, desc);
	}

	public ServiceResult<Project> saveOrUpdate(Long id, Long teamId, String title, String desc) {
		if(teamId<0){
			return new ServiceResult<Project>(-1,null,"team id must more than 0");
		}
		if(StringUtils.isBlank(title)){
			return new ServiceResult<Project>(-2,null,"title is null or empty");
		}
		if(StringUtils.isBlank(desc)){
			return new ServiceResult<Project>(-3,null,"desc is null or empty");
		}
		if(SessionHolder.get().getId()==null){
			return new ServiceResult<Project>(-6,null,"请登录");
		}
		Team team=teamDao.read(Team.class, teamId);
		if(team==null){
			return new ServiceResult<Project>(-5,null,"团队不存在");
		}
		if(!projectDao.read("from Project where title=:title and team.id=:teamId", new Params("title", title),new Params("teamId", team.getId())).isEmpty()){
			return new ServiceResult<Project>(-4,null,"项目重名");
		}
		
		Project project=new Project();
		project.setDesc(desc);
		project.setUidCreate(SessionHolder.get());
		project.setUidManager(SessionHolder.get());
		
		project.setTitle(title);
		project.setTeam(teamDao.read(Team.class, teamId));
		if(id!=null){
			project.setId(id);
			project.setGmtModified(System.currentTimeMillis()/1000);
			projectDao.update(project);
		}else{
			project.setGmtCreate(System.currentTimeMillis()/1000);
			project.setGmtModified(System.currentTimeMillis()/1000);
			projectDao.create(project);
		}
		return new ServiceResult<Project>(1,project);
	}

	public ServiceResult<Project> delete(Project project) {
		// TODO Auto-generated method stub
		int result=projectDao.delete(project);
		if(result==0){
			return new ServiceResult<Project>(1);
		}
		return new ServiceResult<Project>(-1,null,"delete failed");
	}
	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public TeamDao getTeamDao() {
		return teamDao;
	}

	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}

	

}
